﻿using System;

namespace AppConfigVerifier
{
	public delegate void LogDelegate<T>(T sender, Location startLocation, Location endLocation, String message);

	public delegate void GenericLogDelegate<T>(
		T sender, Location startLocation, Location endLocation, String message, LogType type);


	public enum LogType
	{
		Error,
		Warning
	}

	public class Logger<T>
	{
		public event LogDelegate<T> ErrorReceived;
		public event LogDelegate<T> WarningReceived;
		public event GenericLogDelegate<T> LogReceived;

		readonly T sender;
		readonly string content;

		public Logger(T sender, string content)
		{
			this.sender = sender;
			this.content = content;
		}

		public void Send(string message, int startIndex, int endIndex, LogType type)
		{
			Send(message, Location.GetLocation(startIndex, content), Location.GetLocation(endIndex, content), type);
		}

		public void Send(string message, LogType type)
		{
			Send(message, 0, 0, type);
		}

		public void Send(string message, int startIndex, LogType type)
		{
			Send(message, startIndex, content.Length - 1, type);
		}

		public void Send(string message, Location startLocation, Location endLocation, LogType type)
		{
			if (type == LogType.Warning)
			{
				if (WarningReceived != null)
				{
					WarningReceived(sender, startLocation, endLocation, message);
				}
			}
			else if (type == LogType.Error)
			{
				if (ErrorReceived != null)
				{
					ErrorReceived(sender, startLocation, endLocation, message);
				}
			}
			else
			{
				throw new NotSupportedException();
			}

			if (LogReceived != null)
			{
				LogReceived(sender, startLocation, endLocation, message, type);
			}
		}
	}
}
